<template>
  <div style="width: 100%; height: 100%">
    <el-header class="el-header">
      <div class="header-back-container">
        <div class="el-page-header">
          <div class="el-page-header__left" @click="onBack">
            <i class="el-icon-back"></i>
            <div class="el-page-header__title">返回</div>
          </div>
          <div class="el-page-header__content">
            <div class="title-box">
              <div class="title">执行记录</div>
            </div>
          </div>
        </div>
      </div>
    </el-header>
    <el-main style="height: calc(100% - 60px); overflow-y: auto">
      <div class="common-container">
        <div class="header">
          <!-- <div class="header_left"></div> -->
          <div class="header_right">
            <el-select
              size="small"
              v-model="searchSel"
              slot="prepend"
              placeholder="请选择"
              class="el_Select sel_prepend"
            >
              <el-option label="触发设备编码" value="1"></el-option>
              <el-option label="联动规则名称" value="2"></el-option>
            </el-select>
            <el-input
              placeholder="请输入"
              v-model="search_val"
              style="width: 240px; margin-right: 15px"
              size="small"
              maxlength="20"
              clearable
              @clear="clearInput"
              class="el_input"
            >
              <el-button
                slot="append"
                :icon="loadingSearch ? 'el-icon-loading' : 'el-icon-search'"
                @click="searchClick"
              >
              </el-button>
            </el-input>

            <el-date-picker
              class="el_date_picker"
              v-model="value1"
              size="small"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="change"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="content">
          <el-table
            :data="tableData"
            class="table"
            height="400"
            :header-cell-style="headerStyle"
          >
            <el-table-column
              align="center"
              label="序号"
              type="index"
              width="80"
              :index="indexMethod"
            ></el-table-column>
            <el-table-column
              align="center"
              label="联动规则名称"
              prop="ruleName"
              min-width="135"
            ></el-table-column>
            <el-table-column
              align="center"
              label="触发设备编码"
              prop="imei"
              min-width="135"
            ></el-table-column>
            <el-table-column
              align="center"
              label="触发时间"
              prop="logAtStr"
              min-width="135"
            ></el-table-column>
            <el-table-column
              align="center"
              label="触发条件"
              prop="triggerConditionName"
              min-width="135"
            ></el-table-column>
            <!-- <el-table-column align="center" label="操作" min-width="140">
              <template slot-scope="scope">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="删除"
                  placement="bottom"
                >
                  <i
                    @click="delClick(scope.row.id)"
                    class="iconfont shanchu tableBtnIcon"
                  ></i>
                </el-tooltip>
              </template>
            </el-table-column> -->
          </el-table>
          <pagination
            :total="total"
            :page.sync="page"
            :limit.sync="limit"
            @pagination="getData"
            class="pagination"
          />
        </div>
      </div>
    </el-main>
    <!-- <el-footer class="dialog-footer">
      <el-button size="small" @click="closeAddDialog">取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="addSubmit"
        v-preventReClick="1000"
        >确 定</el-button
      >
    </el-footer> -->
  </div>
</template>
<script>
import tableStyleMixin from "@mixin/table-style";
import { mapState, mapMutations } from "vuex";
import {
  reqRegionLevel,
  reqSpaceManager,
  reqProductList,
  reqProductModelPage,
  reqDeviceList,
} from "@/api/api";
import { reqRuleRuleLog, reqRuleRemoveLog } from "@api/rule";
import Pagination from "@components/Pagination.vue";
import { reqByProductKey } from "@/api/rule";
import moment from "moment";

export default {
  name: "",
  components: { Pagination },
  props: ["logId"],
  mixins: [tableStyleMixin],
  data() {
    return {
      searchSel: "1",
      search_val: "",
      loadingSearch: false,
      page: 1,
      limit: 10,
      total: 0,
      tableData: [],

      value1: "",
      // startTime: moment(moment().startOf("day").valueOf()).format(
      //   "YYYY-MM-DD HH:mm:ss"
      // ), //当前时间
      // endTime: moment(moment().valueOf()).format("YYYY-MM-DD HH:mm:ss"), //当前时间
      startTime: "",
      endTime: "",
      //实现时间限制选择在一个月内而且不能超过当前时间
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.cuttentTime = minDate.getTime();
          if (maxDate) {
            this.cuttentTime = "";
          }
        },
        disabledDate: (time) => {
          //获取当前时间戳
          let myDate = new Date();
          let newY = myDate.getFullYear();
          let newM = myDate.getMonth() + 1;
          let newD = myDate.getDate();
          let oldTime =
            newY +
            "-" +
            (newM < 10 ? "0" + newM : newM) +
            "-" +
            (newD < 10 ? "0" + newD : newD) +
            " " +
            "23:59:59";
          // let oldTime="2022-7-5 23:59:59"
          oldTime = new Date(oldTime).getTime(); // 将中国时间转换成毫秒
          // 今天之后的时间不可选
          if (time.getTime() > oldTime) {
            //time.getTime() > Date.now()
            return true;
          }
          //选择日期范围为一个月
          if (this.cuttentTime != "") {
            const one = 30 * 24 * 3600 * 1000;
            const minTime = this.cuttentTime - one;
            const maxTime = this.cuttentTime + one;
            return time.getTime() < minTime || time.getTime() > maxTime;
          }
        },
      },
    };
  },
  computed: {
    ...mapState(["user", "spacesManage"]),
  },
  methods: {
    // 序号
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.limit;
    },
    change(e) {
      if (e && e.length) {
        this.startTime = e[0];
        this.endTime = e[1];
        this.clearInput();
      } else {
        this.startTime = "";
        this.endTime = "";
        this.clearInput();
      }
    },
    clearInput() {
      this.page = 1;
      this.limit = 10;
      this.getData();
    },
    async searchClick() {
      this.page = 1;
      this.limit = 10;
      this.loadingSearch = true;
      await this.getData();
      this.loadingSearch = false;
    },
    // 获取数据的方法
    async getData() {
      let imei = "";
      let ruleName = "";
      this.searchSel == 1
        ? (imei = this.search_val)
        : (ruleName = this.search_val);
      let res = await reqRuleRuleLog(this.page, this.limit, {
        imei,
        ruleName,
        startLogTime: this.startTime,
        endLogTime: this.endTime,
        tenantId: this.user.tenantId,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      let { rows, total } = res.data;
      this.tableData = rows;
      this.total = total || 0;
    },
    // 删除
    async delClick(id) {
      const result = await this.$confirm("是否确认删除该记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {});
      if (result !== "confirm") return;
      const res = await reqRuleRemoveLog(id);
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      this.$modal.notifySuccess("删除成功");
    },
    onBack() {
      this.$parent.isShowRecord = false;
    },
  },
};
</script>

<style scoped lang="less">
@import url("~@views/style/tablePage.less");
@import url("./ruleOperate.less");
.header_right {
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
  display: flex;
  justify-content: flex-end;
}
// .content {
//   margin-top: 10px;
//   height: calc(100% - 50px) !important;
// }
// 查询条件切换
::v-deep .sel_prepend .el-input {
  width: 140px;
}

// 搜索框
::v-deep .el_input .el-input__inner {
  border-left: none !important;
  border-radius: 0px !important;
}

::v-deep .el_Select .el-input__inner {
  border-radius: 0px !important;
}

// 时间控件
::v-deep .el-range-input {
  //   background: @primaryBorderColor3;
  background: none !important;
}
::v-deep .el-range-editor--small .el-range-separator {
  line-height: 32px;
}
::v-deep .el-input__inner {
  background-color: @primaryBorderColor3 !important;
  border: 1px solid @primaryBorder !important;
  //   height: 34px;
  //   line-height: 34px;
}

::v-deep .el-range-editor.el-input__inner {
  padding: 0 0 0 10px;
}

::v-deep .el-date-editor--daterange.el-input__inner {
  width: 400px;
}

::v-deep .el-date-editor .el-range__icon {
  color: @primaryFFFColor5 !important;
}

::v-deep .el-range-separator {
  color: @primaryFFFColor5 !important;
}

:v-deep .el-input__inner,
:v-deep .el-date-editor .el-range-input {
  color: @primaryFFFColor5 !important;

  &::placeholder {
    color: @primaryFFFColor5 !important;
  }

  &::-webkit-input-placeholder {
    /* WebKit browsers 适配谷歌 */
    color: @primaryFFFColor5 !important;
  }

  &:-moz-placeholder {
    /* Mozilla Firefox 4 to 18 适配火狐 */
    color: @primaryFFFColor5 !important;
  }

  &::-moz-placeholder {
    /* Mozilla Firefox 19+ 适配火狐 */
    color: @primaryFFFColor5 !important;
  }

  &:-ms-input-placeholder {
    /* Internet Explorer 10+  适配ie*/
    color: @primaryFFFColor5 !important;
  }
}

::v-deep .el-date-table td div {
  background: #25293c !important;
}
::v-deep .el-popper.el-picker-panel {
  background-color: #25293c !important;
}

::v-deep .popper__arrow::after {
  border-bottom-color: transparent !important;
}
</style>
